package com.woniuxy.uums.service.impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.woniuxy.uums.service.SmsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author ：yolo
 * @date ：Created in 2025/6/17 10:01
 * @description：短信服务实现类
 * @modified By：
 * @version: $
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Value("${ronglianyun.accountSid}")
    private String accountSid;

    @Value("${ronglianyun.authToken}")
    private String authToken;

    @Value("${ronglianyun.appId}")
    private String appId;

    @Value("${ronglianyun.templateId}")
    private String templateId;

    @Override
    public boolean sendSms(String phone, String param1, String param2) {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init("app.cloopen.com", "8883");
        sdk.setAccount(accountSid, authToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);//请求体为json

        // 短信模板参数，按顺序对应模板中的{1}、{2}...
        String[] datas = {
                param1 != null && param1.length() > 4 ? param1.substring(0, 4) : param1,
                param2 != null && param2.length() > 4 ? param2.substring(0, 4) : param2
        };

        HashMap<String, Object> result = sdk.sendTemplateSMS(phone, templateId, datas);//号码，模板ID，模板参数

        if ("000000".equals(result.get("statusCode"))) {
            return true;
        } else {
            System.err.println("短信发送失败: " + result.get("statusMsg"));
            return false;
        }
    }

    @Override
    public boolean sendVerificationCode(String phone, String code) {
        // 发送验证码短信，使用验证码模板
        return sendSms(phone, code, "");
    }


}
